package com.example.exceldemo.utils.poi.v2.subpackage.execl;

import com.example.exceldemo.utils.poi.v2.subpackage.extend.ExportCellMerge;
import com.example.exceldemo.utils.poi.v2.subpackage.extend.ExportCellStyle;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Data
public class ExportRow {
    // 行高
    public float rowHeightInPoints;

    // 表头标识(true为表头)
    public boolean header;

    // 数据
    public List<ExportCell> cells;

    // 自定义导出行的单元格样式
    public ExportCellStyle exportRowCellStyle;

    // 自定义导出行的单元格合并
    public ExportCellMerge exportRowCellMerge;

    public ExportRow appendCells(Object... values) {
        for (Object value : values) {
            appendCell(value, null);
        }
        return this;
    }

    public ExportRow appendCells(Object obj) throws IllegalAccessException {
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            // 私有属性也可以访问
            field.setAccessible(true);
            Object value = field.get(obj);
            appendCell(value == null ? "" : value, null);
        }
        return this;
    }

    public ExportRow appendCell(Object value) {
        appendCell(value, null);
        return this;
    }

    public ExportRow appendCell(Object value, String dateFormat) {
        if (cells == null) {
            cells = new ArrayList<>();
        }
        ExportCell cell = new ExportCell();
        cell.setValue(value);
        cell.setDateFormat(dateFormat);
        cells.add(cell);
        return this;
    }

    public void createNewCells(List<String> list) {
        List<ExportCell> cells = new ArrayList<>();
        for (String s : list) {
            ExportCell cell = new ExportCell();
            cell.setValue(s);
            cells.add(cell);
        }
        this.cells = cells;
    }
}